package com.fpx.open.platform.common.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * HTTP调用工具类
 * 
 */
public class HTTPClient {

    public static String sendPostRequest(String url, Map<String, String> paras) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (Entry<String,String> entry : paras.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String result = sendRequest(httpPost, entity);
        return result;
    }
    
    public static String sendPostRequest(String url, Map<String, String> paras, String mediaType) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        if(mediaType != null){
        	httpPost.setHeader(HttpHeaders.ACCEPT, mediaType);
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (Entry<String,String> entry : paras.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        String result = sendRequest(httpPost, entity);
        return result;
    }

    public static String sendPostRequest(String url, String strReq, String mediaType) throws ClientProtocolException, IOException  {
        HttpPost httpPost = new HttpPost(url);
        if(mediaType != null){
        	httpPost.setHeader("Content-Type", mediaType);
        }
        httpPost.setHeader(HttpHeaders.CONNECTION, "close");
        httpPost.setConfig(RequestConfig.custom().setSocketTimeout(7000).setConnectTimeout(3000).setConnectionRequestTimeout(3000).build());
        StringEntity stringEntity = new StringEntity(strReq, "UTF-8");
        String result = sendRequest(httpPost, stringEntity);
        return result;
    }

    public static String sendPutRequest(String url, String strReq, String contentType) throws Exception {
        HttpPut httpPut = new HttpPut(url);
        StringEntity stringEntity = null;
        stringEntity = new StringEntity(strReq, "UTF-8");
        String result = sendRequest(httpPut, stringEntity);
        return result;
    }

    public static String sendGetRequest(String url, String strReq, String contentType) throws Exception {
        HttpClient httpclient = null;
        HttpGet httpGet = null;
        HttpResponse response = null;
        String result = null;
        try {
            httpclient = HttpClients.createDefault();
            httpGet = new HttpGet(url);
            httpGet.setHeader("Content-Type", contentType);
            httpGet.setHeader("Accept", contentType);
            response = httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
            return result;
        } catch (Exception e) {
            throw new Exception("发送请求服务器端响应出错", e);
        } finally {
            if (httpGet != null) {
                httpGet.abort();
            }
            if (httpclient != null) {
                httpclient.getConnectionManager().shutdown();
            }
        }
    }

    private static String sendRequest(HttpEntityEnclosingRequestBase httpRequest, HttpEntity stringEntity) throws ClientProtocolException, IOException {
    	CloseableHttpClient httpclient = HttpClients.createDefault();
    	httpRequest.setEntity(stringEntity);
    	CloseableHttpResponse response = httpclient.execute(httpRequest);
    	try {
    	    HttpEntity entity = response.getEntity();
    	    return EntityUtils.toString(entity, "UTF-8");
    	} finally {
    	    response.close();
    	    httpclient.close();
    	}
    }
}
